package cn.hairui.fastms.base.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * <p>
 * <h2>视图跳转规则配置</h2>
 *  <ol>配置访问规则，过滤非法权限访问</ol>
 * <h2>修改历史</h2>
 *  <ol>如有修改，请添加修改历史</ol>
 * </p>
 *
 * @author lihairui
 * @date 2020/11/11 16:46
 * @copyright 李海瑞
 */

@Configuration
public class DefaultWebConfig implements WebMvcConfigurer {

    @Autowired
    private DefaultInterceptor defaultInterceptor;

    @Value("${upload_path}")
    private String uploadPath;

    @Value("${virtual_upload_path}")
    private String virtualUploadPath;

    // 这个方法是用来配置静态资源的，比如html，js，css，等等
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(virtualUploadPath + "**").addResourceLocations("file:" + uploadPath);
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

    // 这个方法用来注册拦截器，我们自己写好的拦截器需要通过这里添加注册才能生效
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //配置静态资源 登陆与忘记密码之外其他页面不登录无法访问，因为登陆注册不需要登陆也可以访问
        registry.addInterceptor(defaultInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/login", "/sUser/userLogin", "/error/**", "/forget", "/register")
                .excludePathPatterns("/static/**")
                .excludePathPatterns("/json/**");
    }

}
